import 'package:edu/pages/common/course_list_cell_widget.dart';
import 'package:edu/pages/common/empty_content_widget.dart';
import 'package:edu/pages/common/shopping_cart_button.dart';
import 'package:edu/pages/course/course_list/course_list_page_logic.dart';
import 'package:edu/utils/app_router.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';

class CourseListPage extends StatefulWidget {
  const CourseListPage({super.key});

  @override
  State<CourseListPage> createState() => _CourseListPageState();
}

class _CourseListPageState extends State<CourseListPage> {
  @override
  void initState() {
    super.initState();
    context.read<CourseListPageLogic>().init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(context),
      body: _buildMainBody(context),
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    final title = context.read<CourseListPageLogic>().title;
    return AppBar(
      title: Text(title!),
      centerTitle: true,
      actions: [
        IconButton(
          onPressed: () {
            context.pushNamed(AppRouter.kCourseSearch);
          },
          icon: const Icon(Icons.search_outlined),
        ),
        const ShoppingCartButton(),
        const SizedBox(width: 15),
      ],
    );
  }

  Widget _buildMainBody(BuildContext context) {
    return Selector<CourseListPageLogic, bool>(
      selector: (context, logic) => logic.isCourseEmpty,
      builder: (context, v, child) {
        if (v) {
          return Selector<CourseListPageLogic, Tuple2<bool, bool>>(
            selector: (context, logic) =>
                Tuple2(logic.isLoading, logic.isError),
            builder: (context, v, child) {
              return EmptyContentWidget(
                isLoading: v.item1,
                isError: v.item2,
                reloadAction: () {
                  context.read<CourseListPageLogic>().loadCourseList();
                },
              );
            },
          );
        } else {
          return _buildContentList(context);
        }
      },
    );
  }

  Widget _buildContentList(BuildContext context) {
    return Selector<CourseListPageLogic, Tuple2<int, bool>>(
      selector: (context, logic) =>
          Tuple2(logic.courses.length, logic.canLoadMore),
      builder: (context, value, child) {
        final logic = context.read<CourseListPageLogic>();
        final canLoadMore = value.item2;
        final courses = logic.courses;

        if (canLoadMore) {
          return ListView.builder(
            controller: logic.scrollController,
            itemCount: courses.length + 1,
            itemBuilder: (context, index) {
              if (index == courses.length) {
                return const CourseLoadingCellWidget();
              } else {
                final course = courses[index];
                return CourseListCellWidget(
                  course: course,
                  onTap: () {
                    context.pushNamed(
                      AppRouter.kCourseDetail,
                      queryParameters: {'id': '${course.id}'},
                    );
                  },
                );
              }
            },
          );
        } else {
          return ListView.builder(
            controller: logic.scrollController,
            itemCount: courses.length,
            itemBuilder: (context, index) {
              final course = courses[index];
              return CourseListCellWidget(
                course: course,
                onTap: () {
                  context.pushNamed(
                    AppRouter.kCourseDetail,
                    queryParameters: {'id': '${course.id}'},
                  );
                },
              );
            },
          );
        }
      },
    );
  }
}
